package com.kaishengit.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.AccountDao;
import com.kaishengit.dao.AdminDao;
import com.kaishengit.entity.Account;
import com.kaishengit.entity.Admin;

public class AdminServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String task = request.getParameter("task");
		String method = request.getMethod();
		
		if("login".equals(task)){
			if("GET".equals(method)){
				toSysLogin(request,response);
			}else if("POST".equals(method)){
				sysLogin(request,response);
			}
			
		}else if(task == null||"list".equals(task)){
			
			findAll(request,response);
		}else if("lock".equals(task)){
			
			lock(request,response);
		}else if("unlock".equals(task)){
			
			unlock(request,response);
		}
	}

	private void toSysLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("/WEB-INF/views/sys.jsp").forward(request, response);
	}

	private void unlock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		AccountDao dao = new AccountDao();
		
		Account acc = dao.findById(Integer.parseInt(id));
		
		acc.setEnable(true);
		dao.update(acc);
		request.getRequestDispatcher("admin.jspx?task=list").forward(request,response);
	}

	private void lock(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		AccountDao dao = new AccountDao();
		
		Account acc = dao.findById(Integer.parseInt(id));
		
		acc.setEnable(false);
		dao.update(acc);
		request.getRequestDispatcher("admin.jspx?task=list").forward(request,response);
	}

	private void findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		AccountDao dao = new AccountDao();
		
		List<Account> list = dao.findAll(); 
		request.setAttribute("accList",list);
		
		request.getRequestDispatcher("menu.jsp").forward(request,response);
		
	}

	private void sysLogin(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		String name = request.getParameter("name");
		String pwd = request.getParameter("password");
		
		AdminDao dao = new AdminDao();
		
		Admin admin = dao.findByNameAndPwd(name, pwd);
		
		if(admin != null){
			
			session.setAttribute("curradmin",admin);
			response.sendRedirect("admin.jspx?task=list");
		}else{
			
			response.sendRedirect("sys.jsp?error=200");
		}
	}

}
